import 'package:flutter/material.dart';

import 'package:flutter_note/note/base.dart';
import 'package:flutter_note/note/bean.dart';
import 'package:flutter_note/note/const.dart';
import 'package:flutter_note/note/manager.dart';
import 'package:flutter_note/note/page_login.dart';
import 'package:flutter_note/note/page_update_password.dart';
import 'package:flutter_note/note/page_user_modify.dart';
import 'package:flutter_note/note/utils.dart';
import 'package:flutter_note/note/widgets.dart';

class SettingPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SettingPageState();
  }
}

class _SettingPageState extends BaseState<SettingPage> {
  User _mUser;
  UserManager _userManager;

  @override
  void initState() {
    super.initState();
    _userManager = UserManager.getInstance();
    _mUser = _userManager.getUser();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldkey,
      backgroundColor: color.bg_page,
      body: Column(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Column(
              children: <Widget>[
                Space(Const.SPACE_TOP),
                _userHead(),
                Space(Const.SPACE_TOP),
                _userName(),
                lineHorizontal(dim.x01),
                _userAbout(),
                lineHorizontal(dim.x01),
                _userPassword(),
                Space(Const.SPACE_TOP),
                _logoutWidget(),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _userHead() {
    return GestureDetector(
      onTap: () {
        _modifyUserInfo();
      },
      child: ClipImageView(_mUser.head, dim.x80, dim.x80, Const.IMG_SIZE_HEAD),
    );
  }

  Widget _userName() {
    return SettingView(
      '昵称',
      _mUser.name,
      () {
        _modifyUserInfo();
      },
    );
  }

  Widget _userAbout() {
    return SettingView(
      '简介',
      _aboutText(),
      () {
        _modifyUserInfo();
      },
    );
  }

  String _aboutText() {
    if (EmptyUtils.isEmpty(_mUser.about)) {
      return '';
    } else if (_mUser.about != null && _mUser.about.length > 10) {
      return _mUser.about.substring(0, 10) + "...";
    } else {
      return _mUser.about;
    }
  }

  Widget _userPassword() {
    return SettingView(
      '修改密码',
      '',
      () {
        _modifyPassword();
      },
    );
  }

  Widget _logoutWidget() {
    return RaisedButton(
      color: color.white,
      elevation: 0.0,
      highlightElevation: 0.0,
      padding: EdgeInsets.all(0.0),
      shape: BorderDirectional(),
      onPressed: () async {
        UserManager.getInstance().logout();
        startToPage(LoginPage());
      },
      child: Container(
        height: dim.x50,
        padding: EdgeInsets.only(left: dim.x15, right: dim.x15),
        child: Center(
          child: Text(
            '退出登录',
            style: TextStyle(
              color: color.pink,
              fontSize: dim.t16,
            ),
          ),
        ),
      ),
    );
  }

  //修改用户信息
  void _modifyUserInfo() async {
    var reply = await startPageForResult(UserUpdatePage());
    if (reply is Reply) {
      _mUser = _userManager.getUser();
      showMessage(reply.msg);
    }
  }

  //修改密码
  void _modifyPassword() async {
    var reply = await startPageForResult(ModifyPasswordPage());
    if (reply is Reply) {
      _mUser = _userManager.getUser();
      showMessage(reply.msg);
    }
  }
}
